Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update index before diff-index to check if there are changes #17

Merged
merged 1 commit into from
Sep 12, 2024

Conversation

adamtheturtle
Copy link
Contributor

Thank you @asottile for the tooling and apologies for the earlier spamming.

It is possible to get to a state where diff-index shows that there is a diff, but there is nothing to commit.
For example, when overwriting a file with its current context.

Experiment:

$ echo hello > foo.txt
$ git add foo.txt
$ git diff-index --no-ext-diff HEAD --
:000000 100644 0000000000000000000000000000000000000000 ce013625030ba8dba906f756967f9e9ca394464a A      foo.txt
$ git commit -m "Add foo.txt"
[experiment b237cf0] Add foo.txt
 1 file changed, 1 insertion(+)
 create mode 100644 foo.txt
$ git diff-index --no-ext-diff HEAD --
$ echo hello > foo.txt
$ git diff-index --no-ext-diff HEAD --
:100644 100644 ce013625030ba8dba906f756967f9e9ca394464a 0000000000000000000000000000000000000000 M      foo.txt
$ git commit -a
On branch experiment
nothing to commit, working tree clean
$

This scenario led to a confusing error:

Traceback (most recent call last):
  File "/home/runner/work/_actions/pre-commit-ci/lite-action/v1.0.2/bin/main", line 298, in <module>
    raise SystemExit(main())
                     ^^^^^^
  File "/home/runner/work/_actions/pre-commit-ci/lite-action/v1.0.2/bin/main", line 270, in main
    commit = _make_commit(src_repo=args.src_repo, head=head, clone=clone)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/_actions/pre-commit-ci/lite-action/v1.0.2/bin/main", line 57, in _make_commit
    subprocess.check_call(
  File "/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/subprocess.py", line 413, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '('git', '-c', 'user.name=does-not-matter', '-c', 'user.email=does-not-matter@example.com', '-c', 'protocol.version=2', '-C', '.', 'commit', '--all', '--quiet', '--no-edit', '--no-verify', '--message=hi')' returned non-zero exit status 1.

This change refreshes the index before diff-ing it, avoiding the error.

It is possible to get to a state where diff-index shows that there is a diff,
but there is nothing to commit.
For example, when overwriting a file with its current context.

Experiment:

```
$ echo hello > foo.txt
$ git add foo.txt
$ git diff-index --no-ext-diff HEAD --
:000000 100644 0000000000000000000000000000000000000000 ce013625030ba8dba906f756967f9e9ca394464a A      foo.txt
$ git commit -m "Add foo.txt"
[experiment b237cf0] Add foo.txt
 1 file changed, 1 insertion(+)
 create mode 100644 foo.txt
$ git diff-index --no-ext-diff HEAD --
$ echo hello > foo.txt
$ git diff-index --no-ext-diff HEAD --
:100644 100644 ce013625030ba8dba906f756967f9e9ca394464a 0000000000000000000000000000000000000000 M      foo.txt
$ git commit -a
On branch experiment
nothing to commit, working tree clean
$
```

This scenario led to a confusing error:

```
Traceback (most recent call last):
  File "/home/runner/work/_actions/pre-commit-ci/lite-action/v1.0.2/bin/main", line 298, in <module>
    raise SystemExit(main())
                     ^^^^^^
  File "/home/runner/work/_actions/pre-commit-ci/lite-action/v1.0.2/bin/main", line 270, in main
    commit = _make_commit(src_repo=args.src_repo, head=head, clone=clone)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/_actions/pre-commit-ci/lite-action/v1.0.2/bin/main", line 57, in _make_commit
    subprocess.check_call(
  File "/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/subprocess.py", line 413, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '('git', '-c', 'user.name=does-not-matter', '-c', 'user.email=does-not-matter@example.com', '-c', 'protocol.version=2', '-C', '.', 'commit', '--all', '--quiet', '--no-edit', '--no-verify', '--message=hi')' returned non-zero exit status 1.
```

This change refreshes the index before diff-ing it, avoiding the error.
Copy link
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asottile asottile merged commit 87b886c into pre-commit-ci:main Sep 12, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants